// 混入
import share from "@/api/share.js"
import Util from "@/api/util.js"
import userApi from "@/api/user.js";

export default {
  computed: {
    isLogin(){ // 是否登录
      if(this.$store.state.token || uni.getStorageSync('token')){
        return true
      }else{
        return false
      }
    }
  },
  methods:{
    // 太阳码分享参数处理
    async shareOptions(option){
      if(option){
        let routeData = {}
        if(option.scene){
          const { data } = await share.getShareOption({redisKey: option.scene})
          routeData = data ? Object.assign(option, data) : option
        }else{
          routeData = option
        }
        routeData.idCode && this.$store.commit('LOGIN_SHARE_IDCODE', routeData.idCode) // 缓存起来，登录用于绑定关系

        return routeData
      }else{
        return ''
      }
    },
    // 获取路由
    getRoute() {
      const routes = getCurrentPages()
      if (!routes.length) {
        return {
          route: '',
          query: ''
        }
      }
      // 当前的页面路由
      const currentRoute = routes[routes.length - 1]
      return {
        route: currentRoute.route,
        query: currentRoute.options,
        routes
      }
    },
    // 获取DOM信息
    getUNavbarHeight(domCalss) {
      return new Promise(resolve => {
        const query = uni.createSelectorQuery().in(this);
        query.select(domCalss).boundingClientRect(data => {
          resolve(data)
        }).exec()
      })
    },

    // 效验是否登录
    async isLoSignInFn(option) { // 所有分享链接必须带上shareType=true
      this.$store.commit('SHARE_ROUTE_DATA', {})
      if (option) {
        option = await this.shareOptions(option)
        this.$store.commit('SHARE_ROUTE_DATA', option)
      }
      // if (!this.isLogin) {
      //   if (this.$store.state.shareRouteData.shareType) {
      //     this.navigateTo({ url: '/pagesB/login/index'})
      //   }
      // }
    },

    // 图片剪切的方法
    coverImgHandleFn (url,w,h) { // url图片地址w宽h高
      return Util.coverImgHandle(url,w,h)
    },
    // 单张图片预览
    imgUrlPreviewFn (url) {
      let previewArr = [] // 预览资源数组
      let data = {
        url: url,
      }
      previewArr.push(data)
      wx.previewMedia({
        sources: previewArr,
        current: 0
      })
    },
    // 预览媒体资源
    previewMediaFn (mediaData,current,type,) {
      let previewArr = [] // 预览资源数组
      let current_ = current || 0 // 默认第一个
      let type_ = type || 'image' // 默认图片
      let mediaArr = []
      if (type_=='video') {
        mediaArr.push(mediaData)
      }
      if (type_=='image') {
        mediaArr = mediaData
      }
      mediaArr.forEach((item, i)=>{
        let data = {
          url: item.url,
          type: type_
        }
        if (type_=='video') {
          data = {
            ...data,
            poster: item.thumbnail
          }
        }
        previewArr.push(data)
      })
      wx.previewMedia({
        sources: previewArr,
        current: current_
      })
    },
    // 获取用户信息
    getInfoFn() {
      userApi.getInfo().then(async (res_) => {
        this.$store.commit("USERINFO", res_.data)
      })
    },

    // 存字段判断页面数据是否刷新
    setIsRefreshType(str) {
      this.$store.commit('IS_REFRESH_TYPE', str)
    },
    // 获取判断页面数据是否刷新
    getIsRefreshType () {
      return this.$store.state.isRefreshType
    },
    // 页面参数处理--对象传化成url参数
    stringUrl (obj) {
      let urlParams = ''
      if (Object.keys(obj).length) {
        for (let key in obj) {
          console.log(obj[key]);
          urlParams += `?${key}=${obj[key]}&`
        }
      }
      urlParams = urlParams.substring(0, urlParams.length - 1) // 截掉末尾的&
      return urlParams
    },
    // 路由跳转 start
    navigateTo (object) {
      if(!object.url) return
      if(!this.isLogin){object.url = '/pagesB/login/index'} // 未登录,后期跳转之前存起需要跳转的url,登录后直接进入
      if (getCurrentPages().length > 9) {
        this.redirectTo(object)
      } else {
        if(this.$store.state.throttling){
          this.$store.commit('SET_THROTTLING',1000)

          const routes = getCurrentPages()
          for (let i = (routes.length - 1); i >= 0; i--) {
            if('/'+routes[i].route + this.stringUrl(routes[i].options) === object.url){ // 跳转的页面在路由栈存在并且参数完全相同，直接返回
              uni.navigateBack(routes.length - i)
              return
            }
          }
          uni.navigateTo(object)
        }
      }
    },
    navigateBack (object) {
      uni.navigateBack(object)
    },
    switchTab (object) {
      uni.switchTab(object)
    },
    redirectTo (object) {
      uni.redirectTo(object)
    },
    reLaunch (object) {
      uni.reLaunch(object)
    },
    // 路由跳转 end
    // 节流函数 案例 this.throttle(this.getList,1000,{id = 1})
    throttle(fn, delay = 500, params){
      if(this.$store.state.throttling){
        this.$store.commit('SET_THROTTLING',delay)
        fn(params)
      }
    }
  }
}